package com.myProject.control.medic;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TMedicinal;
import com.myProject.serves.Impl.MedicSevImpl;
import com.myProject.serves.MedicSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "MedicServlet", urlPatterns = "/medic")
public class MedicServlet extends HttpServlet {
    MedicSev ms = new MedicSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:26
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:26
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            addMedic(request, response);
        } else if ("showAllMedic".equalsIgnoreCase(med)) {
            showAllMedic(request, response);
        }
    }

    private void showAllMedic(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        TMedicinal tMedicinal = new TMedicinal();
        List<TMedicinal> list = ms.getMedicList(tMedicinal);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        System.out.println("json = " + json);
        pw.write(json);
    }

    private void addMedic(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("medicName");
        String type = request.getParameter("medicType");
        String medicDisType = request.getParameter("medicDisType");
        String purpose = request.getParameter("purpose");
        TMedicinal t = new TMedicinal();
        t.setDisastertype(medicDisType);
        t.setType(type);
        t.setName(name);
        t.setPurpose(purpose);
        boolean flag = ms.addMedic(t);
        System.out.println("flag = " + flag);
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        String name = request.getParameter("medicName");
        String type = request.getParameter("medicType");
        String medicDisType = request.getParameter("medicDisType");
        TMedicinal t = new TMedicinal();
        t.setDisastertype(medicDisType);
        t.setType(type);
        t.setName(name);
        PageInfo<TMedicinal> show = ms.showMedicByPage(pageNo, pageSize, t);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }
}
